{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## algorithm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def zig_zag_index(k, n):\n",
    "    # upper side of interval\n",
    "    if k >= n * (n + 1) // 2:\n",
    "        i, j = zig_zag_index(n * n - 1 - k, n)\n",
    "        return n - 1 - i, n - 1 - j\n",
    "\n",
    "    # lower side of interval\n",
    "    i = int((np.sqrt(1 + 8 * k) - 1) / 2)\n",
    "    j = k - i * (i + 1) // 2\n",
    "    return (j, i - j) if i & 1 else (i - j, j)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def zig_zag_value(i, j, n):\n",
    "    # upper side of interval\n",
    "    if i + j >= n:\n",
    "        return n * n - 1 - zig_zag_value(n - 1 - i, n - 1 - j, n)\n",
    "\n",
    "    # lower side of interval\n",
    "    k = (i + j) * (i + j + 1) // 2\n",
    "    return k + i if (i + j) & 1 else k + j"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## run"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "n = 10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  5,  6, 14, 15, 27, 28, 44, 45],\n",
       "       [ 2,  4,  7, 13, 16, 26, 29, 43, 46, 63],\n",
       "       [ 3,  8, 12, 17, 25, 30, 42, 47, 62, 64],\n",
       "       [ 9, 11, 18, 24, 31, 41, 48, 61, 65, 78],\n",
       "       [10, 19, 23, 32, 40, 49, 60, 66, 77, 79],\n",
       "       [20, 22, 33, 39, 50, 59, 67, 76, 80, 89],\n",
       "       [21, 34, 38, 51, 58, 68, 75, 81, 88, 90],\n",
       "       [35, 37, 52, 57, 69, 74, 82, 87, 91, 96],\n",
       "       [36, 53, 56, 70, 73, 83, 86, 92, 95, 97],\n",
       "       [54, 55, 71, 72, 84, 85, 93, 94, 98, 99]])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "M = np.zeros((n, n), dtype=int)\n",
    "for i in range(n):\n",
    "    for j in range(n):\n",
    "        M[i, j] = zig_zag_value(i, j, n)\n",
    "M"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  5,  6, 14, 15, 27, 28, 44, 45],\n",
       "       [ 2,  4,  7, 13, 16, 26, 29, 43, 46, 63],\n",
       "       [ 3,  8, 12, 17, 25, 30, 42, 47, 62, 64],\n",
       "       [ 9, 11, 18, 24, 31, 41, 48, 61, 65, 78],\n",
       "       [10, 19, 23, 32, 40, 49, 60, 66, 77, 79],\n",
       "       [20, 22, 33, 39, 50, 59, 67, 76, 80, 89],\n",
       "       [21, 34, 38, 51, 58, 68, 75, 81, 88, 90],\n",
       "       [35, 37, 52, 57, 69, 74, 82, 87, 91, 96],\n",
       "       [36, 53, 56, 70, 73, 83, 86, 92, 95, 97],\n",
       "       [54, 55, 71, 72, 84, 85, 93, 94, 98, 99]])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "M = np.zeros((n, n), dtype=int)\n",
    "for k in range(n * n):\n",
    "    M[zig_zag_index(k, n)] = k\n",
    "M"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
